$.permission={
    //左侧权限树setting初始化
    initLeftSeting:function () {
        var setting = {
            view: {
                dblClickExpand: false,
            },
            data: {
                simpleData: {
                    enable: true,//true / false 分别表示 使用 / 不使用 简单数据模式如果设置为 true，请务必设置 setting.data.simpleData 内的其他参数: idKey / pIdKey / rootPId，并且让数据满足父子关系。
                    idKey:"id",
                    pIdKey:"parentid",
                    rootPId:0
                },
                key: {
                    name: "title"
                }
            },
            callback: {
                onClick: this.onClick
            }
        };
        return setting;
    },
    //加载左侧权限树数据
    initLeftZtree:function () {
        //此地方要稍微注意下，考虑不将本身以及下级节点显示出来，怕选择错误，造成连环套
        $.get("/permission/getpermisTreeData",
            function(zNodes){
                //初始化数结构
                var tree = $.fn.zTree.init($("#"+$.permission.paramer.tree), $.permission.initLeftSeting(), zNodes);
                //默认展开节点
                tree.expandAll(true);
        });
    },
    //左侧点击树节点 时将选中节点返回给页面
    onClick:function () {
        var zTree = $.fn.zTree.getZTreeObj($.permission.paramer.tree);
        var nodes = zTree.getSelectedNodes();
        var title = nodes[0].title;
        var id = nodes[0].id;
        var parmers=new Object();
        parmers.id=id;
        $.permission.loadPermisTable("/permission/permissionList",parmers);
    },
    //当点击左侧树菜单是加载右侧表格信息
    loadPermisTable:function ($url,$parmers) {
        $.post($url, $parmers,'json').success(function(data) {
            if (data) {
                if (data.state == 1) {
                    //$vui.Loading(data.msg,{overlay:true,timeout:2,icon:"icon icon-check-circle-o"});
                    $.permission.loadTemplate(data);
                }else {
                    $vui.Loading(data.msg,{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                }
            } else {
                $vui.Loading("操作失败！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
            }
        }).error(function() {
            $vui.Loading("操作失败，请稍后再试！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
        });
    },
    //加载模板
    loadTemplate:function ($data) {
        var html = template('permis-list', $data);
        $("#"+$.permission.paramer.treeTableList). empty();
        $("#"+$.permission.paramer.treeTableList).html(html);
        $("#"+$.permission.paramer.table).treeTable({expandLevel : 10});
    },
    //左侧添加修改 权限树setting初始化
    initRightSeting:function () {
        var setting = {
            view: {
                dblClickExpand: false
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey:"id",
                    pIdKey:"parentid",
                    rootPId:0
                },
                key: {
                    name: "title"
                }
            },
            callback: {
                onClick: this.selectClick
            }
        };
        return setting;
    },
    //初始化添加修改页面的树节点
    selectPermis:function (id) {
        $.get("/permission/getpermisTreeData?id="+id,
            function(zNodes){
                //初始化数结构
                var tree = $.fn.zTree.init($("#menuTrees"), $.permission.initRightSeting(), zNodes);
                //默认展开节点
                tree.expandAll(true);
                for(var i=0;i<zNodes.length;i++){
                    tree.expandNode(zNodes[i],true,false,false);
                }
                //如果是进入修改页面，定位到当前选中的节点
                var selectNodeId = $("#parentId").val();
                if(selectNodeId!=null){
                    tree.selectNode(tree.getNodeByParam("id",selectNodeId,null));
                }
            });
    },
    //添加修改时赋值
    selectClick:function (e, treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("menuTrees");
        var nodes = zTree.getSelectedNodes();
        var title = nodes[0].title;
        var id = nodes[0].id;
        $("#parentId").val(id);
        $("#parentName").val(title);
        //为使得用户体验更加，选中节点后，影藏数结构
        $.permission.hideMenu();
    },
    //点击搜索框，显示树
    showMenu:function() {
        var parentObj = $("#parenName");
        var parentOffset = parentObj.offset();
        //$("#menuContent").css({left:parentOffset.left + "px", top:parentOffset.top + parentObj.outerHeight() + "px"}).slideDown("fast");
        $("#menuContent").slideDown("fast");
        $("body").bind("mousedown", $.permission.onBodyDown);
    },
    hideMenu:function() {
        $("#menuContent").fadeOut("fast");
        $("body").unbind("mousedown",  $.permission.onBodyDown);
    },
    onBodyDown:function (event) {
        if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
            $.permission.hideMenu();
        }
    },
    //获取权限信息
    loadPermission:function ($url,$id,$parentid) {
        if($.ft.isNotEmpty($id) && $.ft.isNotEmpty($parentid) && $.ft.isNotEmpty($url)){
            $.ajax({
                type: "POST",
                url:$url,
                data:{id:$id,parentid:$parentid},
                async: false,
                error: function(request) {
                    $vui.Loading("操作失败！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                },
                success: function(data) {
                    //接收后台返回的结果
                    if (data) {
                        if (data.state == 1) {
                            $.ft.reset($.permission.paramer.formId);
                            $.ft.jsonInput(data.data,$.permission.paramer.formId)
                            $('#mymodel').modal({
                                keyboard : false,
                                show     : true
                            })
                        }else {
                            $vui.Loading(data.msg,{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                        }
                    } else {
                        $vui.Loading("操作失败！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                    }
                },
            });
        }
    },
    //删除权限信息
    delPermission:function ($url,$id) {
        if($.ft.isNotEmpty($id) && $.ft.isNotEmpty($url)){
            $vui.Dialog({content:"确认删除吗？",callback:function(ok){
                if (ok) {
                    var parmers=new Object();
                    parmers.id=$id;
                    $.post($url, parmers,'json').success(function(data) {
                        if (data) {
                            if (data.state == 1) {
                                $vui.Loading(data.msg,{overlay:true,timeout:2,icon:"icon icon-check-circle-o"});
                                $.permission.initLeftZtree();
                                $.permission.selectPermis('');
                                $.permission.loadPermisTable("/permission/permissionList","");
                            }else {
                                $vui.Loading(data.msg,{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                            }
                        } else {
                            $vui.Loading("操作失败！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                        }
                    }).error(function() {
                        $vui.Loading("操作失败，请稍后再试！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                    });
                }
            }});
        }else {
            $vui.Loading("权限ID或地址不能为空！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
        }
    },
    //表单提交
    submitForm:function () {
        var  url=$('#'+$.permission.paramer.formId).attr("action");
        $.ajax({
            type: "POST",
            url:url,
            data:$('#'+$.permission.paramer.formId).serialize(),
            async: false,
            error: function(request) {
                $vui.Loading("操作失败！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
            },
            success: function(data) {
                //接收后台返回的结果
                if (data) {
                    if (data.state == 1) {
                        $.ft.closeModel($.permission.paramer.modelId)
                        $vui.Loading(data.msg,{overlay:true,timeout:2,icon:"icon icon-check-circle-o"});
                        $.permission.initLeftZtree();
                        $.permission.selectPermis('');
                        $.permission.loadPermisTable("/permission/permissionList","");
                        //重置表单
                        $.ft.reset($.permission.paramer.formId);
                    }else {
                        $vui.Loading(data.msg,{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                    }
                } else {
                    $vui.Loading("操作失败！",{overlay:true,timeout:2,icon:"icon icon-remove-sign"});
                }
            },
        });
    },
    //公共参数
    paramer:{
        //数据表格初始化容器
        table:"treeTable",
        //左侧树 id
        tree:"permisTree",
        //表单id
        formId:"permissionForm",
        //菜单列表
        treeTableList:"treeTableList",
        //对话框id
        modelId:"mymodel",
    }
}